package com.sxkiler.demo.common;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;


public class AlgorithmList {
    static Logger logger = LoggerFactory.getLogger(AlgorithmList.class);
    public static void main(String args[]) throws Exception{
        String listUrl = "https://leetcode-cn.com/api/problems/algorithms/";
        String resultJson = HttpUtil.get(listUrl);
        logger.info(resultJson);
        Map result = JSON.parseObject(resultJson);
        List<Map> questionList = (List)result.get("stat_status_pairs");
//        List<QuestionVo> resultList = new ArrayList<QuestionVo>();

        for(Map item:questionList){
            Map questionMap = (Map)item.get("stat");
            try{
                AlgorithmDetail.generateCode((String)questionMap.get("question__title_slug"));
            }catch (Exception ex){
                logger.error("{},message={}",(String)questionMap.get("question__title_slug"),ex.getMessage(),ex);
                FileUtil.appendString((String)questionMap.get("question__title_slug")+"\r\n","D:/errorQuestion.txt","utf-8");
            }
//            Map difficulty = (Map)item.get("difficulty");
//            QuestionVo questionVo = new QuestionVo();
//            questionVo.setId((Integer)questionMap.get("question_id"));
//            questionVo.setLevel((Integer) difficulty.get("level"));
//            questionVo.setTitleSlug((String)questionMap.get("question__title_slug"));
//            questionVo.setTitle((String)questionMap.get("question__title"));
//            questionVo.setSubmit((Integer)questionMap.get("total_submitted"));
//            questionVo.setAck((Integer)questionMap.get("total_acs"));
//            resultList.add(questionVo);
        }
//        resultList.sort(Comparator.comparing(QuestionVo::getLevel));
//        FileUtil.writeLines(resultList,new File("D:/AlgorithmTest.txt"),"UTF-8");
//        logger.info("result={}",result);
    }

}
